package hamaster.jpbc.dao.impl;

import hamaster.jpbc.IDKey;
import hamaster.jpbc.User;
import hamaster.jpbc.dao.IDKeyDAO;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class IDKeyDAOHibernateImpl extends CommonDAOHibernateImpl implements IDKeyDAO {

	public IDKeyDAOHibernateImpl() {
	}

	@Override
	public IDKey get(User owner) {
		Session session = factory.openSession();
		Criteria criteria = session.createCriteria(IDKey.class).
			add(Restrictions.eq("owner", owner)).
			setFetchSize(1);
		List<?> result = criteria.list();
		return result.size() == 0 ? null : (IDKey) result.get(0);
	}

	@Override
	public List<IDKey> getAll(User owner) {
		Session session = factory.openSession();
		Criteria criteria = session.createCriteria(IDKey.class).
			add(Restrictions.eq("owner", owner)).
			addOrder(Order.desc("genDate"));
		@SuppressWarnings("unchecked")
		List<IDKey> result = criteria.list();
		return result.size() != 0 ? result : null;
	}
}
